package com.axjy.util;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

@Component
@Aspect
@Slf4j
public class SystemAdvice {

    @Before("execution(* com.axjy.service..*.add*(..))")
    public void before(JoinPoint joinPoint){
        log.debug("dfsafdasdfasfasfda");
        Object[] objs=  joinPoint.getArgs();
        if(objs.length>=1){
            try {
                Field field= objs[0].getClass().getDeclaredField("creationDate");
                field.setAccessible(true);
                field.set(objs[0], LocalDateTime.now());
            } catch (NoSuchFieldException | IllegalAccessException e) {
                log.error("改add方法第一个参数对象不存在creationDate属性!",e);
            }
        }
    }
}
